<?php
/**
 * Created by PhpStorm.
 * User: alice
 * Date: 2015/12/28
 * Time: 12:26
 */

/**
 * Class Involved_NewResourceModel
 */

class Involved_NewResourceModel extends BaseModel{

    protected $table = 'new_resource';


    /**
     * 商城产品列表
     * @param $paramObj
     * @return mixed
     */

    public function NewResourceList($paramObj){

        $db = DB::table($this->table);
        #筛选
        //关键字
        if (!empty($paramObj->city)) {
            $db->where('place', 'like', "%{$paramObj->city}%");
        }
        //品类
        if (property_exists($paramObj,'category') && !empty($paramObj->category)) {
            $db->where('category','like', "%{$paramObj->category}%");
        }
        //品名
        if (property_exists($paramObj,'brand') && !empty($paramObj->brand)) {
            $db->where('classes','like', "%{$paramObj->brand}%");
        }
        //钢厂
        if (property_exists($paramObj,'factory') && !empty($paramObj->factory)) {
            $db->where('factory','like', "%{$paramObj->factory}%");
        }
        //公司名
        if (property_exists($paramObj,'company') && !empty($paramObj->company)) {
            $db->where('company','like', "%{$paramObj->company}%");
        }
        #查询
        $goods =  $db
            ->select('new_resource.product_id','new_resource.contact_name','new_resource.category','new_resource.classes','new_resource.factory','new_resource.content','new_resource.down_address','supplier.company','new_resource.down_num')
            ->leftJoin('supplier','supplier.supplier_id','=','new_resource.user_id')
            ->where('new_resource.status',1)
            ->orderBy('new_resource.created','desc')
            ->paginate(20);
            return $goods;

    }

    /**
     * API现货列表
     * @param $paramObj
     * @return mixed
     */
    public function GetApiNewResourceList($paramObj)
    {
        $offset = ($paramObj->page-1) * HostEnum::PAGE_NUM;

        $db = DB::table($this->table);
      /*  if(empty($paramObj->key_word) && $paramObj->classes == 'all' && $paramObj->category == 'all'){
            $db->where('category',17);
        };*/
        #筛选
        //关键字
        if (!empty($paramObj->key_word)) {
            $db->where('product_name', 'like', "%{$paramObj->key_word}%");
        }

        //分类
        if (property_exists($paramObj,'category') && !empty($paramObj->category) && $paramObj->category != 'all') {
            $db->where('category', $paramObj->category);
        }
        //品类
        if (property_exists($paramObj,'classes') && !empty($paramObj->classes) && $paramObj->classes != 'all') {
            $db->where('classes', $paramObj->classes);
        }
        //材质
        if (property_exists($paramObj,'quality') && !empty($paramObj->quality) && $paramObj->quality != 'all') {
            $db->where('quality', $paramObj->quality);
        }
        //规格
        if (property_exists($paramObj,'spec') && !empty($paramObj->spec) && $paramObj->spec != 'all') {
            $db->where('spec', $paramObj->spec);
        }
        //等级
        if (property_exists($paramObj,'level') && !empty($paramObj->level) && $paramObj->level != 'all') {
            $db->where('level', $paramObj->level);
        }
        //产地
        if (property_exists($paramObj,'origin') && !empty($paramObj->origin) && $paramObj->origin != 'all') {
            $db->where('place', $paramObj->origin);
        }
        //仓储地
        if (property_exists($paramObj,'storage') && !empty($paramObj->storage) && $paramObj->storage != 'all') {
            $db->where('storage', $paramObj->storage);
        }
        //品牌
        if (property_exists($paramObj,'brand') && !empty($paramObj->brand) && $paramObj->brand != 'all') {
            $db->where('brand', $paramObj->brand);
        }


        #排序
        if ($paramObj->sort_order == 'all') {
            $db->orderBy('created', 'desc');
        }
        if ($paramObj->sort_order == 'price_desc') {
            $db->orderBy('price', 'desc');
        }
        if ($paramObj->sort_order == 'price_asc') {
            $db->orderBy('price', 'asc');
        }
        if ($paramObj->sort_order == 'sales') {
            $db->orderBy('sales', 'desc');
        }
        #查询
        $goods =  $db
            ->select('product_id','product_name','product_img','spec','storage','price','created','start_num','unit','stock','address','place')
            ->where('status',1)
            ->skip($offset)->take(HostEnum::PAGE_NUM)
            ->get();
        return $goods;

    }

    /**
     * 现货产品详情
     * @param $paramObj
     * @return mixed
     */

    public function NewResourceDetails($paramObj){

        #查询
        return DB::table($this->table)
            ->where('product_id',$paramObj->product_id)
            ->first();

    }

    /**
     * 根据用户ID，查现货
     * @param $user_id
     * @return mixed
     */

    public function UserNowResourceList($user_id){

        #查询
        return DB::table($this->table)
            ->select('new_resource.product_id','new_resource.contact_name','new_resource.down_num','new_resource.status','new_resource.created','new_resource.down_address','supplier.company')
            ->leftJoin('supplier','supplier.supplier_id','=','new_resource.user_id')
            ->where('user_id',$user_id)
            ->orderBy('created','desc')
            ->paginate(10);
    }

    public function AdminNewResourceList(){

        $db = DB::table($this->table);
   /*     #筛选
        //关键字
        if (!empty($paramObj->key_word)) {
            $db->where('product_name', 'like', "%{$paramObj->key_word}%");
        }
        //仓储地
        if (property_exists($paramObj,'storage') && !empty($paramObj->storage)) {
            $db->where('storage', $paramObj->storage);
        }*/

        #查询
        $goods =  $db
            ->select('new_resource.product_id','new_resource.content','new_resource.contact_name','new_resource.status','new_resource.category','new_resource.classes','new_resource.factory','new_resource.content','new_resource.created','new_resource.down_address','supplier.company')
            ->leftJoin('supplier','supplier.supplier_id','=','new_resource.user_id')
            ->orderBy('created','desc')
            ->get();
        return $goods;

    }



    /**
     * API根据用户ID，查现货
     * @param $paramObj
     * @return mixed
     */
    public function GetApiUserNewResourceList($paramObj){
        $offset = ($paramObj->page-1) * HostEnum::PAGE_NUM;
        #查询
        return DB::table($this->table)
            ->select('product_id','product_name','product_img','spec','storage','price','created','start_num','unit','stock','address','place')
            ->where(['user_id'=>$paramObj->user_id,'status'=>$paramObj->status])
            ->skip($offset)->take(HostEnum::PAGE_NUM)
            ->orderBy('created','desc')
            ->get();
    }

    /**
     * API根据用户ID，状态查数量
     * @param $paramObj
     * @return mixed
     */
    public function GetApiUserNewResourceListCount($user_id,$status){
        #查询
        return DB::table($this->table)
            ->select(DB::raw('count(*) as count, status'))
            ->where(['user_id'=>$user_id])
            ->where('status', '!=', $status)
            ->groupBy('status')
            ->get();
    }

    //未审核现货数量
    public function NewResourceNum(){
        return  DB::table($this->table)
            ->select('product_id')
            ->where('status',2)
            ->count();

    }
}
